import React from 'react'
import { connect } from 'dva'
import styles from './index.css'
import Button from 'antd/lib/button'
import { Chart, Geom, Axis, Tooltip, Legend, Coord, Label, Facet } from 'bizcharts'
import 'antd/lib/button/style/css'
import { Row, Col } from 'antd'
import { DataView, DataSet } from '@antv/data-set'

// 数据源
const data = [
    {genre: 'Sports', sold: 275, income: 2300},
    {genre: 'Strategy', sold: 115, income: 667},
    {genre: 'Action', sold: 120, income: 982},
    {genre: 'Shooter', sold: 350, income: 5271},
    {genre: 'Other', sold: 150, income: 3710}
]

// 定义度量
const cols = {
    sold: {alias: '销售量'},
    genre: {alias: '游戏种类'}
}

const data2 = [
    {item: '事例一', count: 40},
    {item: '事例二', count: 21},
    {item: '事例三', count: 17},
    {item: '事例四', count: 13},
    {item: '事例五', count: 9}
]
const dv = new DataView()
dv.source(data2).transform({
    type: 'percent',
    field: 'count',
    dimension: 'item',
    as: 'percent'
})
const cols2 = {
    percent: {
        formatter: val => {
            val = (val * 100) + '%'
            return val
        }
    }
}
// 渲染图表
const data3 = [
    {year: 2007, area: '亚太地区', profit: 7860 * 0.189},
    {year: 2007, area: '非洲及中东', profit: 7860 * 0.042},
    {year: 2007, area: '拉丁美洲', profit: 7860 * 0.025},
    {year: 2007, area: '中欧和东欧', profit: 7860 * 0.018},
    {year: 2007, area: '西欧', profit: 7860 * 0.462},
    {year: 2007, area: '北美', profit: 7860 * 0.265},
    {year: 2011, area: '亚太地区', profit: 7620 * 0.539},
    {year: 2011, area: '非洲及中东', profit: 7620 * 0.065},
    {year: 2011, area: '拉丁美洲', profit: 7620 * 0.065},
    {year: 2011, area: '中欧和东欧', profit: 7620 * 0.034},
    {year: 2011, area: '西欧', profit: 7620 * 0.063},
    {year: 2011, area: '北美', profit: 7620 * 0.234}
]

const cols3 = {
    percent: {
        formatter: val => {
            return (val * 100).toFixed(2) + '%'
        }
    }
}
let id = 0

const data4 = [
    {month: 'Jan', Tokyo: 7.0, London: 3.9},
    {month: 'Feb', Tokyo: 6.9, London: 4.2},
    {month: 'Mar', Tokyo: 9.5, London: 5.7},
    {month: 'Apr', Tokyo: 14.5, London: 8.5},
    {month: 'May', Tokyo: 18.4, London: 11.9},
    {month: 'Jun', Tokyo: 21.5, London: 15.2},
    {month: 'Jul', Tokyo: 25.2, London: 17.0},
    {month: 'Aug', Tokyo: 26.5, London: 16.6},
    {month: 'Sep', Tokyo: 23.3, London: 14.2},
    {month: 'Oct', Tokyo: 18.3, London: 10.3},
    {month: 'Nov', Tokyo: 13.9, London: 6.6},
    {month: 'Dec', Tokyo: 9.6, London: 4.8}
]
const ds = new DataSet()
const dv4 = ds.createView().source(data4)
dv4.transform({
    type: 'fold',
    fields: ['Tokyo', 'London'], // 展开字段集
    key: 'city', // key字段
    value: 'temperature', // value字段
})
const cols4 = {
    month: {
        range: [0, 1]
    }
}

function IndexPage () {
    return (
        <div>
            <Row gutter={16}>
                <Col className="gutter-row" span={12}>
                    <div className="gutter-box">
                        <Chart width={400} height={400} data={data} scale={cols} padding={[20, 0, 20, 10]}>
                            <Axis name="genre"/>
                            <Axis name="sold"/>
                            <Legend position="top" dy={-20}/>
                            <Tooltip />
                            <Geom type="interval" position="genre*sold" color="genre"/>
                        </Chart>
                    </div>
                </Col>
                <Col className="gutter-row" span={12}>
                    <div className="gutter-box">
                        <Chart height={400} width={400} data={dv} scale={cols2} padding={[20, 0, 20, 10]} forceFit>
                            <Coord type='theta' radius={0.75}/>
                            <Axis name="percent"/>
                            <Legend position='right' offsetY={-window.innerHeight / 2 + 120} offsetX={-100}/>
                            <Tooltip
                                showTitle={false}
                                itemTpl='<li><span style="background-color:{color};" class="g2-tooltip-marker"></span>{name}: {value}</li>'
                            />
                            <Geom
                                type="intervalStack"
                                position="percent"
                                color='item'
                                tooltip={['item*percent', (item, percent) => {
                                    percent = percent * 100 + '%'
                                    return {
                                        name: item,
                                        value: percent
                                    }
                                }]}
                                style={{lineWidth: 1, stroke: '#fff'}}
                            >
                                <Label content='percent' formatter={(val, item) => {
                                    return item.point.item + ': ' + val
                                }}/>
                            </Geom>
                        </Chart>
                    </div>
                </Col>
            </Row>
            <Row gutter={16}>
                <Col className="gutter-row" span={12}>
                    <div className="gutter-box">
                        <Chart height={400} width={400} data={data3} padding={10} forceFit>
                            <Tooltip showTitle={false}/>
                            <Facet type='rect' fields={['year']} padding={50} colTitle={{
                                offsetY: -30,
                                style: {
                                    fontSize: 18,
                                    textAlign: 'center',
                                    fill: '#999'
                                }
                            }}
                                   eachView={(view, facet) => {
                                       const dv2 = new DataView()
                                       dv2.source(data3)
                                           .transform({
                                               type: 'percent',
                                               field: 'profit',
                                               dimension: 'area',
                                               as: 'percent'
                                           })
                                       view.source(dv2, {
                                           percent: {
                                               formatter: val => {
                                                   return (val * 100).toFixed(2) + '%'
                                               }
                                           }
                                       })
                                       view.coord('theta', {
                                           innerRadius: 0.35
                                       })

                                       view.intervalStack()
                                           .position('percent')
                                           .color('area')
                                           .label('percent', {
                                               offset: -8
                                           })
                                           .style({
                                               lineWidth: 1,
                                               stroke: '#fff'
                                           })
                                   }}
                            />
                        </Chart>
                    </div>
                </Col>
                <Col className="gutter-row" span={12}>
                    <div className="gutter-box">
                        <Chart height={400} width={400} data={dv4} scale={cols4} forceFit>
                            <Legend />
                            <Axis name="month"/>
                            <Axis name="temperature" label={{formatter: val => `${val}°C`}}/>
                            <Tooltip crosshairs={{type: 'y'}}/>
                            <Geom type="line" position="month*temperature" size={2} color={'city'} shape={'smooth'}/>
                            <Geom type='point' position="month*temperature" size={4} shape={'circle'} color={'city'}
                                  style={{stroke: '#fff', lineWidth: 1}}/>
                        </Chart>
                    </div>
                </Col>
            </Row>
        </div>
    )
}

IndexPage.propTypes = {}

export default connect()(IndexPage)
